package wql.aaa.service.impl;

import com.aaa.entity.TbBottomArticle;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import wql.aaa.service.TbBottomArticleService;
import wql.aaa.dao.TbBottomArticleMapper;
import org.springframework.stereotype.Service;
import wql.aaa.vo.AddBottomArticle;
import wql.aaa.vo.BottomFormData;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 咕噜咕噜
* @description 针对表【tb_bottom_article】的数据库操作Service实现
* @createDate 2023-02-28 14:54:42
*/
@Service
public class TbBottomArticleServiceImpl extends ServiceImpl<TbBottomArticleMapper, TbBottomArticle>
    implements TbBottomArticleService{

    @Autowired
    private TbBottomArticleMapper tbBottomArticleMapper;

    @Override
    public Result queryAll(int currentPage, int pageSize, BottomFormData bottomFormData) {

       // PageHelper.startPage(currentPage,pageSize);

        List<TbBottomArticle> tbBottomArticles = tbBottomArticleMapper.find(bottomFormData);

        List<TbBottomArticle> firsts = firsts(tbBottomArticles);

       // PageInfo<TbBottomArticle> info = new PageInfo<>(firsts);

        return new Result(2000,"查询成功",firsts);

    }

    @Override
    public Result deleteById(String id) {
        int i = tbBottomArticleMapper.deleteById(id);
        if (i>0){
            return new Result(2000,"删除成功");
        }else {
            return new Result(5000,"删除失败");
        }

    }

    @Override
    public Result updateIsDisabled(Integer isDisable,String id) {
        tbBottomArticleMapper.change(isDisable,id);
        return new Result(2000,"修改状态成功");
    }

    @Override
    public Result updateArtical(TbBottomArticle tbBottomArticle) {
        int i = tbBottomArticleMapper.updateById(tbBottomArticle);
        if (i>0){
            return new Result(2000,"修改成功");
        }else {
            return new Result(5000,"修改失败");
        }
    }

    @Override
    public Result add(AddBottomArticle addBottomArticle) {
        TbBottomArticle article = new TbBottomArticle();
        article.setName(addBottomArticle.getName());
        article.setParentId(addBottomArticle.getParentId());
        Date date = new Date();
        article.setGmtCreate(date);
        article.setGmtModified(date);
        article.setIsDeleted(0);
        article.setIsDisable(0);
        int insert = tbBottomArticleMapper.insert(article);
        if (insert>0){
            return new Result(2000,"添加成功");
        }else {
            return new Result(5000,"添加失败");
        }
    }

    private List<TbBottomArticle> firsts(List<TbBottomArticle> menus){
        List<TbBottomArticle> firsts = new ArrayList<>();
        for (TbBottomArticle menu : menus) {
            if (menu.getParentId().equals("0")){
                firsts.add(menu);
            }
        }
        for (TbBottomArticle first : firsts) {
            List<TbBottomArticle> children = new ArrayList<>();
            for (TbBottomArticle mm : menus) {
                if (first.getId().equals(mm.getParentId())){
                    children.add(mm);
                }
            }
            first.setChildren(children);
        }
        return firsts;
    }
}




